package com.jintian.hnsj_metting.api

import android.app.Application
import com.hjq.http.EasyConfig
import com.hjq.http.config.IRequestBodyStrategy
import com.hjq.http.config.IRequestServer
import com.hjq.http.model.CacheMode
import com.hjq.http.model.RequestBodyType
import com.jintian.hnsj_metting.BuildConfig
import com.jintian.hnsj_metting.util.DeviceInfo
import okhttp3.OkHttpClient

const val HOST = "https://hnsjgc.com.cn:10081"

class RequestServer : IRequestServer {
    override fun getHost(): String {
        return "$HOST/api/"
    }

    override fun getBodyType(): IRequestBodyStrategy {
        return RequestBodyType.JSON
    }

    override fun getCacheMode(): CacheMode {
        return CacheMode.NO_CACHE
    }
}

class ApiConfig {

    companion object {
        fun init(app: Application) {
            val okHttpClient: OkHttpClient = OkHttpClient.Builder()
                .build()

            EasyConfig.with(okHttpClient) // 是否打印日志
                .setLogEnabled(BuildConfig.DEBUG) // 设置服务器配置（必须设置）
                .setServer(RequestServer()) // 设置请求处理策略（必须设置）
                .setHandler(RequestHandler(app)) // 设置请求重试次数
                .setRetryCount(2) // 添加全局请求参数
                .addHeader("mToken", DeviceInfo.mToken)
                .into()
        }
    }
}
